package com.cmsky.open.web.controller.admin;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cmsky.open.core.base.BasePager;
import com.cmsky.open.core.base.BaseSpringController;
import com.cmsky.open.core.util.JsonUtils;
import com.cmsky.open.security.model.ActionsInfo;
import com.cmsky.open.security.service.SecurityService;
import com.cmsky.open.shop.manager.ShopInfoManager;
import com.cmsky.open.shop.model.ShopInfo;
import com.cmsky.open.shop.service.ShopLoginService;
import com.cmsky.open.shop.vo.query.ShopInfoQuery;

@Controller("admin.Index")
@RequestMapping("/admin")
public class IndexController extends BaseSpringController{
	protected static final Logger logger = Logger.getLogger(IndexController.class);
	private ShopLoginService shopLoginService;
	private SecurityService securityService;

	public void setShopLoginService(ShopLoginService shopLoginService) {
		this.shopLoginService = shopLoginService;
	}

	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}
	
	protected List<ActionsInfo> getMenu(List<ActionsInfo> source){
		List<ActionsInfo> menu = new LinkedList<ActionsInfo>();
		for(ActionsInfo a : source){
			if(a.getType()==1){
				if(!menu.contains(a)){
					menu.add(a);
					a.setChildren(new LinkedList<ActionsInfo>());
				}
			}else if(a.getType() == 2){
				for(ActionsInfo b : menu){
					if(a.getPid() == b.getId()){
						b.getChildren().add(a);
					}
				}
			}
		}
		return menu;
	}
	/** 
	 * 进入默认页面,后台登录暂时简要处理
	 **/
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelAndView view = new ModelAndView("/admin/index");
		Map<String,String> adminParams = new HashMap<String,String>();
		adminParams.put("title", "后台管理系统");
		adminParams.put("systemName", "Java后台管理系统");
		adminParams.put("copyright", "jandyfish");
		view.addObject("adminPage", adminParams);
		
		ShopInfo shoper = this.shopLoginService.getLoginedShoper(request, response);
		List<ActionsInfo> actions=null;
		if(null!=shoper){
			if(this.shopLoginService.isSuper(shoper)){
				actions = this.securityService.findAll();
			}else{
				actions = this.securityService.findByRoleId(shoper.getRoleId());
			}
			if(actions!=null){
				List<ActionsInfo> menus = getMenu(actions);
				view.addObject("shoper", shoper);
				view.addObject("menus", menus);
				logger.info("index<|>"+shoper+"<|>"+menus);
			}
		}
		
		return view;
	}
	/** 
	 * 进入默认页面
	 **/
	@RequestMapping("/desc")
	public ModelAndView desc(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelAndView view = new ModelAndView("/admin/desc");
		Map<String,String> adminParams = new HashMap<String,String>();
		adminParams.put("title", "后台管理系统");
		adminParams.put("systemName", "Java后台管理系统");
		adminParams.put("copyright", "jandyfish");
		view.addObject("adminPage", adminParams);
		return view;
	}
	
	@RequestMapping("/tolg")
	public ModelAndView tolg(HttpServletRequest request,HttpServletResponse response) throws Exception {
		return new ModelAndView("/admin/lg");
	}
	
	@RequestMapping("/lgIn")
	@ResponseBody
	public String lgIn(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String result=null;
		String lgName = request.getParameter("lgName");
		String passwd = request.getParameter("passWd");
		ShopInfo info = this.shopLoginService.lgIn(request, response, lgName, passwd);
		if(null!=info){
			result = "ok";
		}else{
			result = "no";
		}
		return result;
	}
	
	@RequestMapping("/lgOut")
	public String lgOut(HttpServletRequest request,HttpServletResponse response) throws Exception {
		this.shopLoginService.lgOut(request, response);
		return "forward:/admin/index.do";
	}
}
